/**
 * Created by wzm on 14-10-8.
 */
//cc.SPRITE_DEBUG_DRAW = 1;
var canChangePage = true;
var curScene = null;
var MainScene = cc.Scene.extend({
    listener: null,
    accelListener: null,
    currentIndex: 0,
    sceneList: [],

    ctor: function () {
        this._super();

    },
    onEnter: function () {
        this._super();
        this.initUI();
        this.addTouch();
        this.initHideEvent();
        curScene = this;

        musicNum = '';
        playMusic(true ,1);

        //alert(cc.winSize.width);
        //alert('window:' + window.innerWidth);
        //alert('gameCanvas:' + document.getElementById('gameCanvas').clientWidth);

        //alert(document.getElementById('gameCanvas').height);
        //alert('Cocos2dGameContainer:' + document.getElementById('Cocos2dGameContainer').clientWidth);

		//musicFadeIn();
//        this.addAccel();
    },
    initUI: function () {

        //背景色
        var bgLayer = new cc.LayerColor(cc.color(11, 15, 36, 255));
        this.addChild(bgLayer, 0);

        /*水印
        var logo = new cc.Sprite(res.logo1);
        logo.setPosition(cc.pAdd(cc.visibleRect.center, cc.p(0, 0)));
        logo.setOpacity(50);
        this.addChild(logo, 1);
        */

        this.arrow = new cc.Sprite(res.arrow);
        this.arrow.scale = 0.7;
        this.arrow.setPosition(cc.pAdd(cc.visibleRect.center, cc.p(cc.visibleRect.center.x - 10,0)));
        var posX = this.arrow.x;
        var arrowAction = cc.repeatForever(cc.sequence(cc.spawn(cc.moveTo(0.8, cc.p( posX - 150,this.arrow.y)).easing(cc.easeIn(0.5)), cc.fadeOut(1)), cc.delayTime(0.8), cc.callFunc(function () {
            this.arrow.x = this.arrow.x + 150;
            this.arrow.opacity = 255;
        }, this)));
        this.arrow.runAction(arrowAction);
        this.addChild(this.arrow, 1);

        /****/
        this.menuItemToggle = new cc.MenuItemToggle(new cc.MenuItemImage(res.m1), new cc.MenuItemImage(res.m2), this.toggleMusicCallback, this);
        this.menuItemToggle.setPosition(cc.pAdd(cc.visibleRect.topRight, cc.p( - 70, -70)));
		this.menuItemToggle.scale = 0.7;
        var togglemenu = new cc.Menu(this.menuItemToggle);
        togglemenu.anchorX = 0;
        togglemenu.anchorY = 0;
        togglemenu.x = 0;
        togglemenu.y = 0;
		
        this.addChild(togglemenu, 1);


        this.animLayer = new cc.Layer();
        this.addChild(this.animLayer);
        this.sceneList.push(new Layer1());
        this.sceneList.push(new Layer2());
        this.sceneList.push(new Layer3());
        this.sceneList.push(new Layer4());
        this.sceneList.push(new Layer5());
        for (var i = 0; i < this.sceneList.length; i++) {
            var scene = this.sceneList[i];
            scene.anchorX = 0;
            scene.anchorY = 0;
            scene.x = 0;//cc.winSize.width;
            scene.y = 0;
            if (this.currentIndex != i) {
                scene.setVisible(false);
                //scene.x = this.animLayer.width;
            }
            this.animLayer.addChild(scene, this.sceneList.length - i);
            if (this.currentIndex == i && scene.appear) {
                scene.appear();
            }
        }

        this.canChange(false);
    },
    initHideEvent: function () {
        cc.eventManager.addCustomListener(cc.game.EVENT_SHOW, function () {
            playMusic(true);
        });
        cc.eventManager.addCustomListener(cc.game.EVENT_HIDE, function () {
            playMusic(false);
        });

    },
    toggleMusicCallback: function (sender) {
        if (sender.getSelectedIndex() == 0) {
            playMusic(true);
            musicStatus = true;
        } else {
            playMusic(false);
            musicStatus = false;
        }
    },
    togleArrow: function (status) {
        if (status) {
            this.arrow.visible = true;
        }
        else {
            this.arrow.visible = false;
        }
    },
    addTouch: function () {
        var self = this;
        self.listener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            startPosX: 0,
            onTouchBegan: function (touch, event) {
                this.startPosX = touch.getLocation().x;
                return true;
            },
            onTouchMoved: function (touch, event) {

            },
            onTouchEnded: function (touch, event) {

                if (canChangePage) {
                    var delta = this.startPosX - touch.getLocation().x;
                    if (delta > 15 && self.currentIndex < self.sceneList.length - 1) {
                        self.changePage(++self.currentIndex, true);
                    } else if (delta < -15 && self.currentIndex > 0) {
                       // self.changePage(--self.currentIndex, false);
                    }else if(self.currentIndex == self.sceneList.length - 1){
                        //最后一页重新加载
                        playMusic(0);
                        cc.director.runScene(_MainScene = new MainScene());
                        _MainScene.sceneList = [];

                    }
                }
            },
            onTouchCancelled: function (touch, event) {

            }
        });
        cc.eventManager.addListener(self.listener, self);
    },
    addAccel: function () {
        var self = this;
        cc.inputManager.setAccelerometerInterval(1 / 30);
        cc.inputManager.setAccelerometerEnabled(true);
        this.accelListener = {
            event: cc.EventListener.ACCELERATION,
            callback: function (acc, event) {
                for (var i = 0; i < self.sceneList.length; ++i) {
                    self.sceneList[i].accelEvent(acc, event);
                }
            }
        }
        cc.eventManager.addListener(this.accelListener, self);
    },
    changePage: function (index, next) {
        this.canChange(false);
        var scene = next ? this.sceneList[index - 1] : this.sceneList[index + 1];

        var nextPage = function () {
            scene.visible = false;
            this.sceneList[index].visible = true;

            this.sceneList[index].appear();
        };
        if (scene) {
            scene.disappear(nextPage, this);
        }
    },
    canChange:function (is) {
        canChangePage = is;
        this.togleArrow(is);
    }
});


var act_shake = function (speed,move,repeat) {

    speed = speed || 0.05;
    move = move || cc.p(8, 8);

    var move = cc.moveBy(speed, move);
    var move_back = move.reverse();
    var move_seq = cc.sequence(move, move_back);
    if(!repeat)
    return move_seq.repeatForever();
    return move_seq.repeat(repeat);
}


var Layer1 = cc.Layer.extend({
    ctor: function () {
        this._super();
        this.initUI();
    },
    onEnter: function () {
        this._super();
        //++_MainScene.currentIndex;
        //this.appear();
        if(_MainScene.currentIndex == 0)
        this.addTouch();
    },
    scale:function () {
        return cc.winSize.width / this.bg.width;
    },
    initUI: function () {


        this.accLayer = new cc.Layer();
        this.accLayer.anchorX = 0;
        this.accLayer.anchorY = 0;
        this.accLayer.x = 0;
        this.accLayer.y = 0;
        this.addChild(this.accLayer);

        this.bg = new cc.Sprite(res.bg1);
        //this.bg.setOpacity(50);
        this.bg.anchorX = 0;
        this.bg.anchorY = 0;
        this.bg.scale = this.scale();
        //this.bg.scaleY = cc.winSize.height / (this.bg.height/2);
        this.addChild(this.bg, 0);



        this.rocket = new cc.Sprite();
        //this.rocket.setContentSize();
        this.rocket.scale = this.scale();
        this.rocket.setPosition(cc.pAdd(cc.visibleRect.bottom, cc.p(0, (this.bg.height/2)*this.scale()/4 + 30)));
        this.addChild(this.rocket, 1);

        //火箭
        this.p1_1 = new cc.Sprite(res.p1_1);
        this.rocket.addChild(this.p1_1 ,1);

        //火焰
        this.p1_2 = new cc.Sprite(res.p1_2);
        this.p1_2.anchorY = 1;
        this.p1_2.scale = 0;
        this.p1_2.setPosition( cc.p(15, -215));
        this.rocket.addChild(this.p1_2 ,0);

        //笑脸
        this.p1_6 = new cc.Sprite(res.p1_6);
        this.p1_6.scale = 1.4;
        this.p1_6.setOpacity(0);
        this.p1_6.setPosition( cc.p(5, -25));
        this.rocket.addChild(this.p1_6 ,1);

        //烟雾
        this.p1_3 = new cc.Sprite(res.p1_3);
        this.p1_3.setOpacity(150);
        this.p1_3.scale = 0.1;
        this.p1_3.setPosition(cc.pAdd(cc.visibleRect.bottom, cc.p(0,  150)));
        this.addChild(this.p1_3 ,2);

        //阴影
        this.p1_4 = new cc.Sprite(res.p1_4);
        this.p1_4.scale = this.scale();
        this.p1_4.setPosition(cc.pAdd(cc.visibleRect.bottom, cc.p(10, this.rocket.y - this.p1_1.height*this.scale()/2 + 10)));
        this.addChild(this.p1_4 ,0);

        //按钮
        this.p1_5 = new cc.Sprite(res.arrow);
        this.p1_5.scale = this.scale() * 0.8;
        this.p1_5.setPosition(cc.pAdd(cc.visibleRect.center, cc.p(150,  50)));
        this.addChild(this.p1_5 ,2);

        //
        var label = cc.LabelTTF.create("点击屏幕 发射火箭", "Arial", 30);
        label.setPosition(30 ,-50);
        this.p1_5.addChild(label, 2);

        //手指动画
        var act = cc.sequence(new cc.FadeOut(1) , new cc.FadeIn(1)).repeatForever();
        this.p1_5.runAction(act);
        label.runAction(act.clone());


        this.rocketStatus = 0; //0 初始；1 启动；2 运行；3 暂停；4 飞走


    },
    appear:function () {
        //this.startRocket();
    },

    getBezier:function () {
        var     start = cc.p(this.rocket.getPosition())  ,
            ctrl1 = cc.p(cc.pAdd(this.rocket.getPosition() , cc.p(0,200))),
            ctrl2 = cc.p(cc.pAdd(this.rocket.getPosition() , cc.p(0,350))),
            end = cc.p(cc.pAdd(this.rocket.getPosition() , cc.p(250,300)));

        var dn = new cc.DrawNode();
        //drawCubicBezier(origin, control1, control2, destination, segments, lineWidth, color)
        dn.drawCubicBezier(start , ctrl1 ,ctrl2 ,end ,29,2,cc.color( 233, 64, 65, 155));
        //this.addChild(dn, 1);

        var Segment1 = new cc.DrawNode();
        Segment1.drawSegment(start , ctrl1,1,cc.color( 233, 64, 65, 155));
        //this.addChild(Segment1, 1);

        var Segment2 = new cc.DrawNode();
        Segment2.drawSegment(ctrl2 , end ,1,cc.color( 233, 64, 65, 155));
        //this.addChild(Segment2, 1);


        var controlPoints = [ ctrl1,
            ctrl2,
            end ];

        return bezierForward = cc.bezierTo(1, controlPoints);
    },

    onEnd:function () {

        cc.eventManager.removeListener(this.listener);
        _MainScene.changePage(++_MainScene.currentIndex, true);
    },


    //火箭飞行
    runRocket:function () {


        var _this = this;
        var bg_scale = _this.scale();


        var st = setInterval(function () {

            if(_this.rocketStatus != 2){clearInterval(st);return;}

            if(_this.rocket.getPositionY() <= cc.visibleRect.center.y - 150){
                //第一阶段 ： 火箭离地 飞到屏幕中间
                _this.rocket.runAction(cc.moveBy(0.3,0,100) )
            }else if( (_this.bg.getPositionY()+_this.bg.height * bg_scale) >= cc.visibleRect.top.y + 170){
                //第二阶段 ： 火箭不动 背景往下走
                _this.bg.runAction(cc.moveBy(0.3,0,-150));
            }else {
                //第三阶段 ： 火箭延贝塞尔曲线飞走 消失
                console.log((new Date()).getTime());
                musicFadeOut();


                _this.rocketStatus = 4;
                _this.rocket.runAction(
                    cc.sequence(
                        cc.spawn(cc.moveBy(0.4,0,150)//,new cc.scaleBy(1,0.05,0.05)
                        ),

                        cc.callFunc(function () {
                            _this.rocket.runAction(
                                cc.sequence(
                                cc.spawn(
                                _this.getBezier(),
                                new cc.scaleTo(1,0,0),
                                new cc.rotateBy(1,106,106)

                            ),cc.callFunc(function () {
                                        _this.onEnd();
                                    })
                                )
                                );
                        })
                    ));
                clearInterval(st);
            }

        },300);

        //阴影动画
        this.p1_4.runAction(
            cc.sequence(cc.delayTime(0.5),cc.spawn(

                new cc.FadeTo(0.3, 0),
                new cc.scaleTo(0.3,2,1)
            ),
            cc.callFunc(function () {
                //变脸
                this.p1_6.runAction(new cc.FadeIn(1));
            },this)
            )
            )
    },

    //火箭启动
    startRocket:function () {

        this.rocketStatus = 1;

        var _this = this;

        //背景动画
        this.bg.runAction(act_shake(0,0,20));
        //烟雾动画
        _this.p1_3.runAction(
            cc.spawn(
                new cc.scaleTo(2,2,2),
                cc.sequence(new cc.FadeTo(0.5, 255) ,cc.delayTime(0.5), new cc.FadeTo(1, 0))
            )
        );
        //火焰动画
        _this.p1_2.runAction(new cc.scaleTo(2,1,1));
        _this.p1_2.runAction(act_shake(0,cc.p(4,8)));

        setTimeout(function () {

            //if(_this.listener.touchStatus != 1){_this.rocketStatus = 3;return;}

            _this.rocketStatus = 2;
            _this.runRocket();
        },1500);
    },
    addTouch: function () {
        var self = this;
        self.listener = cc.EventListener.create({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            startPosY: 0,
            startTime: 0,
            touchStatus:0,
            onTouchBegan: function (touch, event) {
                var _this = this;
                _this.touchStatus = 1;
                self.p1_5.visible = false;
                //this.startPosY = touch.getLocation().y;
                if(self.rocketStatus == 0){
                    var d = new Date();
                    this.startTime = d.getTime();
                    setTimeout(function () {
                        //if(!_this.touchStatus)return false;
						self.p1_5.visible = false;
                        self.startRocket();
						
                    },100);

                }else if(self.rocketStatus == 3){
                    self.rocketStatus = 2;
                    //self.runRocket();
                }


                return true;
            },
            onTouchMoved: function (touch, event) {

            },
            onTouchEnded: function (touch, event) {
                this.touchStatus = 0;
                self.p1_5.visible = true;
                if(self.rocketStatus == 2){
                    //self.rocketStatus = 3;

                }
            },
            onTouchCancelled: function (touch, event) {

            }
        });
        cc.eventManager.addListener(self.listener, self);
    },
    accelEvent: function (acc, event) {
        if (this.visible) {
            movArea(acc, this.accLayer);
        }
    },
    disappear: function (callback, target) {
            if (target && callback) {
                callback.call(target);
            }

    }

});
var Layer2 = cc.Layer.extend({
    ctor: function () {
        this._super();
    },
    onEnter: function () {
        this._super();
        
		this.initUI();
        //this.appear();
    },
    initUI: function () {
        this.accLayer = new cc.Layer();
        this.accLayer.anchorX = 0;
        this.accLayer.anchorY = 0;
        this.accLayer.x = 0;
        this.accLayer.y = 0;
        this.addChild(this.accLayer);

        this.bg = new cc.Sprite(res.bg3);
        this.bg.scaleX = cc.winSize.width / this.bg.width;
        this.bg.scaleY = cc.winSize.height / this.bg.height;
        this.bg.setPosition(cc.visibleRect.center);
        //this.bg.setOpacity(50);
        this.addChild(this.bg, 0);

        var attr = {anchorY:1,x:this.bg.width/2,y:this.bg.height-5};
        this.p2_1a = new cc.Sprite(res.p2_1a);
        this.p2_1a.attr(attr);
        this.bg.addChild(this.p2_1a);
        this.p2_1b = new cc.Sprite(res.p2_1b);
        this.p2_1b.attr(attr);
        this.p2_1b.setOpacity(0);
        this.bg.addChild(this.p2_1b);

        var attr = {anchorX:1,x:this.bg.width - 8,y:800};
        this.p2_2a = new cc.Sprite(res.p2_2a);
        this.p2_2a.attr(attr);
        this.bg.addChild(this.p2_2a);
        this.p2_2b = new cc.Sprite(res.p2_2b);
        this.p2_2b.attr(attr);
        this.p2_2b.setOpacity(0);
        this.bg.addChild(this.p2_2b);

        var attr = {anchorX:0,x: 8,y:790};
        this.p2_3a = new cc.Sprite(res.p2_3a);
        this.p2_3a.attr(attr);
        this.bg.addChild(this.p2_3a);
        this.p2_3b = new cc.Sprite(res.p2_3b);
        this.p2_3b.attr(attr);
        this.p2_3b.setOpacity(0);
        this.bg.addChild(this.p2_3b);

        var attr = {anchorY:0,x:this.bg.width/2,y:380};
        this.p2_4a = new cc.Sprite(res.p2_4a);
        this.p2_4a.attr(attr);
        this.bg.addChild(this.p2_4a);
        this.p2_4b = new cc.Sprite(res.p2_4b);
        this.p2_4b.attr(attr);
        this.p2_4b.setOpacity(0);
        this.bg.addChild(this.p2_4b);

        var attr = {anchorY:0,x:this.bg.width/2,y:0};
        this.p2_5a = new cc.Sprite(res.p2_5a);
        this.p2_5a.attr(attr);
        this.bg.addChild(this.p2_5a);
        this.p2_5b = new cc.Sprite(res.p2_5b);
        this.p2_5b.attr(attr);
        this.p2_5b.setOpacity(0);
        this.bg.addChild(this.p2_5b);


    },
    accelEvent: function (acc, event) {
        if (this.visible) {
            movArea(acc, this.accLayer);
        }
    },
    appear: function () {



        var bgAction = cc.sequence(new cc.FadeIn(0.5), cc.callFunc(function () {console.log((new Date()).getTime());
            playMusic(0);
			musicNum = '2';
        if(musicStatus){
			playMusic(1,1);
			musicFadeIn();
		}
            this.p2_1b.runAction(cc.sequence(cc.delayTime(0.3),new cc.fadeIn(0.5)));
            this.p2_2b.runAction(cc.sequence(cc.delayTime(2.3),new cc.fadeIn(0.5)));
            this.p2_3b.runAction(cc.sequence(cc.delayTime(4.3),new cc.fadeIn(0.5)));
            this.p2_4b.runAction(cc.sequence(cc.delayTime(6.3),new cc.fadeIn(0.5)));
            this.p2_5b.runAction(cc.sequence(cc.delayTime(8.3),new cc.fadeIn(0.5)));


        }, this), cc.delayTime(9), cc.callFunc(function () {

            _MainScene.canChange(true);
        }, this));
        this.bg.runAction(bgAction);
        //_MainScene.changePage(++_MainScene.currentIndex, true);
    },
    disappear: function (callback, target) {

        _MainScene.sceneList[2].visible = true;

        this.runAction(
          cc.sequence( cc.moveBy(1,-this.width ,0) ,cc.callFunc(function () {
              if (target && callback) {
                  callback.call(target);
              }
          }))
        );

    }
});
var Layer3 = cc.Layer.extend({
    ctor: function () {
        this._super();
    },
    onEnter: function () {
        this._super();
        this.initUI();
//        this.appear();
    },
    initUI: function () {

        this.accLayer = new cc.Layer();
        this.accLayer.anchorX = 0;
        this.accLayer.anchorY = 0;
        this.accLayer.x = 0;
        this.accLayer.y = 0;
        this.addChild(this.accLayer);

        this.bg = new cc.Sprite(res.bg3);
        this.bg.scaleX = cc.winSize.width / this.bg.width;
        this.bg.scaleY = cc.winSize.height / this.bg.height;
        this.bg.setPosition(cc.visibleRect.center);
        //this.bg.setOpacity(50);
        this.addChild(this.bg, 0);


        var attr = {anchorY:1,anchorX:0,x:8,y:this.bg.height-8 };
        this.p3_1a = new cc.Sprite(res.p3_1a);
        this.p3_1a.attr(attr);
        this.bg.addChild(this.p3_1a);
        this.p3_1b = new cc.Sprite(res.p3_1b);
        this.p3_1b.attr(attr);
        this.p3_1b.setOpacity(0);
        this.bg.addChild(this.p3_1b);

        var attr = {anchorY:1,anchorX:1,x:this.bg.width - 10,y:this.bg.height-10 };
        this.p3_5a = new cc.Sprite(res.p3_5a);
        this.p3_5a.attr(attr);
        this.bg.addChild(this.p3_5a);
        this.p3_5b = new cc.Sprite(res.p3_5b);
        this.p3_5b.attr(attr);
        this.p3_5b.setOpacity(0);
        this.bg.addChild(this.p3_5b);

        var attr = {anchorX:1,x:this.bg.width - 8,y:705};
        this.p3_2a = new cc.Sprite(res.p3_2a);
        this.p3_2a.attr(attr);
        this.bg.addChild(this.p3_2a);
        this.p3_2b = new cc.Sprite(res.p3_2b);
        this.p3_2b.attr(attr);
        this.p3_2b.setOpacity(0);
        this.bg.addChild(this.p3_2b);

        var attr = {anchorX:0,x: 8,y:705 };
        this.p3_3a = new cc.Sprite(res.p3_3a);
        this.p3_3a.attr(attr);
        this.bg.addChild(this.p3_3a);
        this.p3_3b = new cc.Sprite(res.p3_3b);
        this.p3_3b.attr(attr);
        this.p3_3b.setOpacity(0);
        this.bg.addChild(this.p3_3b);

        var attr = {anchorY:0,x:this.bg.width/2,y:10};
        this.p3_4a = new cc.Sprite(res.p3_4a);
        this.p3_4a.attr(attr);
        this.bg.addChild(this.p3_4a);
        this.p3_4b = new cc.Sprite(res.p3_4b);
        this.p3_4b.attr(attr);
        this.p3_4b.setOpacity(0);
        this.bg.addChild(this.p3_4b);

    },
    accelEvent: function (acc, event) {
        if (this.visible) {
            movArea(acc, this.accLayer);
        }
    },
    appear: function () {//20,50,80,100


        var action = cc.sequence(new cc.FadeIn(0.3),
            cc.callFunc(function () {

                this.p3_1b.runAction(cc.sequence(cc.delayTime(0.3),new cc.fadeIn(0.5)));
                this.p3_5b.runAction(cc.sequence(cc.delayTime(2.3),new cc.fadeIn(0.5)));
                this.p3_2b.runAction(cc.sequence(cc.delayTime(4.3),new cc.fadeIn(0.5)));
                this.p3_3b.runAction(cc.sequence(cc.delayTime(6.3),new cc.fadeIn(0.5)));
                this.p3_4b.runAction(cc.sequence(cc.delayTime(8.3),new cc.fadeIn(0.5)));
            }, this),
            cc.delayTime(8), cc.callFunc(function () {

                _MainScene.canChange(true);
        }, this));
        this.bg.runAction(action);//_MainScene.changePage(++_MainScene.currentIndex, true);
    },
    disappear: function (callback, target) {

        _MainScene.sceneList[3].visible = true;

        this.runAction(
            cc.sequence( cc.moveBy(1,-this.width ,0) ,cc.callFunc(function () {
                if (target && callback) {
                    callback.call(target);
                }
            }))
        );
    }
});


var Layer4 = cc.Layer.extend({
    ctor: function () {
        this._super();
    },
    onEnter: function () {
        this._super();
        this.initUI();
//        this.appear();
    },
    initUI: function () {

        this.accLayer = new cc.Layer();
        this.accLayer.anchorX = 0;
        this.accLayer.anchorY = 0;
        this.accLayer.x = 0;
        this.accLayer.y = 0;
        this.addChild(this.accLayer);

        this.bg = new cc.Sprite(res.bg_9);
        this.bg.scaleX = cc.winSize.width / this.bg.width * 2;
        this.bg.scaleY = cc.winSize.height / this.bg.height;
        this.bg.setPosition( cc.winSize.width -(this.bg.width - 540) *this.bg.scaleX , cc.winSize.height -(this.bg.height - 850) *this.bg.scaleY );
        this.bg.setOpacity(50);this.addChild(this.bg, 0);
        this.bg.anchorX = 540/this.bg.width;
        this.bg.anchorY = 850/this.bg.height;

        this.p8_1 = new cc.Sprite(res.p8_1);
        this.p8_1.scale = 0;
        this.p8_1.setPosition( 960 ,this.bg.height - 840);
        this.bg.addChild(this.p8_1);

        this.p8_2 = new cc.Sprite(res.p8_2);
        this.p8_2.scale = 1.5;
        this.p8_2.setPosition( 300 ,this.bg.height - 300);
        this.bg.addChild(this.p8_2);

        this.p8_1a = new cc.Sprite(res.p8_1);
        this.p8_1a.scale = 0;
        this.p8_1a.setPosition( 960 ,this.bg.height - 840);
        this.bg.addChild(this.p8_1a);

        this.p8_1b = new cc.Sprite(res.p8_1);
        this.p8_1b.scale = 0;
        this.p8_1b.setPosition( 960 ,this.bg.height - 840);
        this.bg.addChild(this.p8_1b);

        //烟圈动画
        this.p8_1.act = cc.sequence(cc.delayTime(0.3),
            cc.spawn( new cc.scaleTo(3,1.2,1.2) ,new cc.fadeTo(2 ,50) ,cc.moveBy(3,350,900) ) ,
            cc.spawn(new cc.scaleTo(0,0,0) ,new cc.fadeTo(0 ,255) ,cc.moveTo(0,960 ,this.bg.height - 840) ) );
        this.p8_1.act.repeatForever();
        this.p8_1.runAction(this.p8_1.act);
        var _this = this;
        setTimeout(function () {
            _this.p8_1a.runAction(_this.p8_1.act.clone());
        },1100);

        setTimeout(function () {
            _this.p8_1b.runAction(_this.p8_1.act.clone());
        },2200);

        //航天员动画
        this.p8_2.act = cc.sequence( cc.spawn(new cc.rotateBy(2.5 , -50,-50) ,cc.moveBy(2.8,-30,20)) ,cc.spawn(new cc.rotateBy(2.5 , 40,40) , cc.moveBy(2.8,30,-20)) );
        this.p8_2.runAction(this.p8_2.act.repeatForever());



        this.menuItemToggle = new cc.MenuItemToggle(new cc.MenuItemImage(res.p9_1), new cc.MenuItemImage(res.p9_1a), this.btnCallback, this);
        this.menuItemToggle.setPosition(cc.pAdd(cc.visibleRect.center, cc.p( 0, -150)));
        this.menuItemToggle.setOpacity(0);
        this.menuItemToggle.scale = cc.winSize.height / this.bg.height * 1.6;
        var togglemenu = new cc.Menu(this.menuItemToggle);
        togglemenu.anchorX = 0;
        togglemenu.anchorY = 0;
        togglemenu.x = 0;
        togglemenu.y = 0;
        this.addChild(togglemenu, 1);

    },
    accelEvent: function (acc, event) {
        if (this.visible) {
            movArea(acc, this.accLayer);
        }
    },
    appear: function () {//20,50,80,100

        var action = cc.sequence(
            new cc.FadeIn(1) ,
            cc.moveBy(4,this.bg.width/2 * this.bg.scaleX ,0),
            cc.delayTime(2),
            cc.spawn(cc.scaleTo(5,1.5,1.5) , cc.moveTo(5,cc.visibleRect.center)),
            cc.delayTime(1),
            cc.callFunc(function () {
            /**/
                this.menuItemToggle.runAction(new cc.fadeIn(0.5));
        }, this), cc.delayTime(0.8), cc.callFunc(function () {

            //_MainScene.canChange(true);
        }, this));
        this.bg.runAction(action);
    },
    disappear: function (callback, target) {

        this.menuItemToggle.runAction(new cc.FadeOut(0.7));
        var bgAction = cc.sequence(cc.spawn(new cc.FadeOut(0.5)), cc.callFunc(function () {

            this.p8_1.runAction(new cc.FadeOut(0.5));
            this.p8_2.runAction(new cc.FadeOut(0.5));
        }, this), cc.delayTime(0.8), cc.callFunc(function () {

            if (target && callback) {
                callback.call(target);
            }
        }, this));
        this.bg.runAction(bgAction);
    },
    btnCallback:function () {
        var _this = this;
        setTimeout(function () {
            _this.menuItemToggle.setSelectedIndex(0);
            _MainScene.changePage(++_MainScene.currentIndex, true);
        },150);

        //
    }
});



var Layer5 = cc.Layer.extend({
    ctor: function () {
        this._super();
    },
    onEnter: function () {
        this._super();
        this.initUI();
//        this.appear();
    },
    initUI: function () {

        this.accLayer = new cc.Layer();
        this.accLayer.anchorX = 0;
        this.accLayer.anchorY = 0;
        this.accLayer.x = 0;
        this.accLayer.y = 0;
        this.addChild(this.accLayer);

        this.bg = new cc.Sprite(res.p10_a);
        this.bg.scaleX = cc.winSize.width / this.bg.width;
        this.bg.scaleY = cc.winSize.height / this.bg.height;
        this.bg.setPosition(cc.visibleRect.center);
        this.bg.setOpacity(50);this.addChild(this.bg, 0);

        this.p10_d = new cc.Sprite(res.p10_d);
        this.p10_d.scaleX = cc.winSize.width / this.bg.width;
        this.p10_d.scaleY = cc.winSize.height / this.bg.height;
        this.p10_d.setPosition(cc.visibleRect.center);
        this.p10_d.setOpacity(0);this.addChild(this.p10_d, 0);

        this.p10_b = new cc.Sprite(res.p10_b);
        this.p10_b.scaleX = cc.winSize.width / this.bg.width;
        this.p10_b.scaleY = cc.winSize.height / this.bg.height;
        this.p10_b.setPosition(cc.visibleRect.center);
        this.p10_b.setOpacity(0);this.addChild(this.p10_b, 0);

        this.p10_c = new cc.Sprite(res.p10_c);
        this.p10_c.scaleX = cc.winSize.width / this.bg.width;
        this.p10_c.scaleY = cc.winSize.height / this.bg.height;
        this.p10_c.setPosition(cc.visibleRect.center);
        this.p10_c.setOpacity(0);this.addChild(this.p10_c, 0);





        this.p10 = function (n ,y) {
            var p = new cc.Sprite(res['p10_'+n]);
            p.anchorX = 0.5;
            p.setOpacity(0);
            p.setPosition(this.bg.width/2,y);
            this.bg.addChild(p);

            p.runAction(cc.sequence(cc.delayTime(n*1.5) ,cc.fadeIn(0.8)));
        }




    },
    accelEvent: function (acc, event) {
        if (this.visible) {
            movArea(acc, this.accLayer);
        }
    },
    appear: function () {//20,50,80,100


        var action = cc.sequence(new cc.FadeIn(0.5), cc.callFunc(function () {
            /*

            this.p10(1,800);
            this.p10(2,700);
            this.p10(3,580);
            this.p10(4,480);
            this.p10(5,400);
            */
            this.p10_b.runAction(cc.sequence(cc.delayTime(0.8) ,cc.fadeIn(1)));
            this.p10_c.runAction(cc.sequence(cc.delayTime(2.5) ,cc.fadeIn(2)));
            this.p10_d.runAction(cc.sequence(cc.delayTime(4) ,cc.fadeIn(2)));
        }, this), cc.delayTime(0.8), cc.callFunc(function () {

            canChangePage = true;
        }, this));
        this.bg.runAction(action);
    },
    disappear: function (callback, target) {

        var bgAction = cc.sequence(cc.spawn(new cc.FadeOut(0.5)), cc.callFunc(function () {


        }, this), cc.delayTime(0.8), cc.callFunc(function () {

            if (target && callback) {
                callback.call(target);
            }
        }, this));
        this.bg.runAction(bgAction);
    }
});














var movArea = function (acc, node) {
    var curx = node.x + 20 * acc.x;
    var cury = node.y + 20 * acc.y;
    node.x = Math.abs(curx) < 7 ? curx : node.x;
    node.y = Math.abs(cury) < 7 ? cury : node.y;

}
/************************************************************************************************************************************/




function log(s) {
    console.log(s);
}
